<?php
class Search_Index {
    private $_lucene;
    public function __construct() {
      $this->_lucene = new Search_Lucene(); 
    }
    
    public function clear() {
      $this->_lucene->clearIndex(); 
    }
    
    public function getInfo() {
      return $this->_lucene->getInfo(); 
    }
    
    public function optimize() {
      return $this->_lucene->optimize(); 
    }
    
    public function rebuildIndex() {
        $this->_lucene->clearIndex();
        $this->_lucene->optimize();
        
        $config = array(
            array('module'=>'page', 'model'=>'Page'),
            array('module'=>'news', 'model'=>'News'),
//            array('module'=>'catalog', 'model'=>'Сatalog'),
        );
        
        
        foreach ($config as $part)
        {
            Loader::includeModuleModels($part['module']);
            $DAO = new $part['model']();
            $index = $DAO->getSearchIndex();
            foreach ($index as $item)
            {   
                $this->_lucene->addDocument($item['title'], $item['text'], $item['teaser'], $item['route'], $item['type'], $item['tag'], $item['image']);
            }
        }
        
        
        $this->_lucene->optimize();
    }
    
    public function search($query) {
      return $this->_lucene->search($query);
    }
}

